package com.zzq.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.zzq.constant.MessageConstant;
import com.zzq.dao.MemberMapper;
import com.zzq.dao.OrderMapper;
import com.zzq.dao.OrdersettingMapper;
import com.zzq.entity.Result;
import com.zzq.pojo.Member;
import com.zzq.pojo.Order;
import com.zzq.pojo.OrderExample;
import com.zzq.pojo.Ordersetting;
import com.zzq.service.OrderService;
import com.zzq.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.Map;

/**
 * 幼儿猿大班
 * 2021/7/27
 */
@Service(interfaceClass = OrderService.class)
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private OrdersettingMapper ordersettingMapper;
    @Autowired
    private MemberMapper memberMapper;

    @Override
    @Transactional
    public Result saveOrder(Map map) throws Exception {
        String  name = (String) map.get("name");
        String  sex = (String) map.get("sex");
        String  idcard = (String) map.get("idCard");
        String orderdate = (String) map.get("orderDate");
        String phonenumber = (String) map.get("telephone");
        String setmealId = (String) map.get("setmealId");

        // 1.根据日期查找当天是否开团
       Ordersetting ordersetting= ordersettingMapper.findByOrderDate(orderdate);

        if (ordersetting==null) {
            return new Result(false, MessageConstant.SELECTED_DATE_CANNOT_ORDER);
        }else{
            // 2.查看预约设置的可预约总人数和已预约人数
            Integer number = ordersetting.getNumber();
            Integer reservations = ordersetting.getReservations();
            if (number<=reservations) {
                return new Result(false,MessageConstant.ORDER_FULL);
            }else{
                // 3.根据身份证号查看是否是会员  不是会员，第一次添加，自动转成和会员添加到会员表中
                Integer mId=memberMapper.findMemberByIdCard(idcard);
                if (mId==null) {
                    // mId为null，不是会员，首次添加
                    Member member = new Member(name, sex, idcard, phonenumber, new Date());
                    memberMapper.insert(member);
                    mId = member.getId();
                }
                   // 4.查看当前日期当前用户是否已经预约了同一个套餐
                OrderExample orderExample=new OrderExample();
                orderExample.createCriteria().andMemberIdEqualTo(mId)
                        .andOrderdateEqualTo(DateUtils.parseString2Date(orderdate))
                        .andSetmealIdEqualTo(Integer.parseInt(setmealId));
                long l = orderMapper.countByExample(orderExample);
                if(l>0){
                    return new Result(false,MessageConstant.HAS_ORDERED);
                }else{
                    // 4.向order表插入数据
                    Order order = new Order(null, mId, DateUtils.parseString2Date(orderdate), "微信预约", "未出游", Integer.parseInt(setmealId));
                    orderMapper.insert(order);
                    Integer orderId = order.getId();
                    // 5.将orderSetting表中的reservents加1（在mapper中做的加1操作）
                    ordersettingMapper.updateByOrderDate(orderdate);
                    return new Result(true,MessageConstant.ORDER_SUCCESS,orderId);
                }
                }

            }
        }

    @Override
    public Map<String, Object> findById(Integer id) {
        return orderMapper.findById(id);
    }
}
